package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.haohao.myview.FixItemListView;
import com.example.haohao.myview.expandView.MyExpandActivity;
import com.example.list.bigbang.BigBangActivity;
import com.example.list.note.NoteActivity;
import com.example.list.swiperefresh.SwipeRefreshActivity;
import com.example.mvptest.JokeActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import mvp.MVPActivity;

public class ListViewActivity extends AppCompatActivity {

    private FixItemListView fixListview;
    MyListAdapter adapter;
    private List<MyClassBean> myClassList=new ArrayList<>();
    private Toast mToast;
    private EditText listEt;
    int intputtimes=0;
    private TextView tv_space;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.colorRed));
        setContentView(R.layout.activity_list_view);


        tv_space = (TextView) findViewById(R.id.tv_line_letterSpace);
        tv_space.setLineSpacing(1, (float) 1.3);
        tv_space.setLetterSpacing((float) 0.05);

        fixListview = (FixItemListView) findViewById(R.id.fixListview);

        myClassList.add(new MyClassBean("mvp", MVPActivity.class));
        myClassList.add(new MyClassBean("mvp2", JokeActivity.class));
        myClassList.add(new MyClassBean("AddView", AddViewActivity.class));
        myClassList.add(new MyClassBean("AddView2,动态修改视图", AddViewActivity2.class));
        myClassList.add(new MyClassBean("刷新", SwipeRefreshActivity.class));
        myClassList.add(new MyClassBean("BigBang", BigBangActivity.class));
        myClassList.add(new MyClassBean("popupwindow2", MyExpandActivity.class));
        myClassList.add(new MyClassBean("btnbg", BtnBgActivity.class));
        myClassList.add(new MyClassBean("Note", NoteActivity.class));

        adapter = new MyListAdapter(this, myClassList);
        fixListview.setAdapter((ListAdapter) adapter);
        fixListview.setFixItemCount(5);
        fixListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectPositon(true,position);
                listEt.setText(myClassList.get(position).getName());
            }
        });
        listEt = (EditText) findViewById(R.id.list_et);

        listEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if ( (s.toString().length() >=6 && count==1)) {
                    //限制输入6位
                    listEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
                    System.out.println("手动输入");
                }else {
                    //取消限制
                    listEt.setFilters(new InputFilter[0]);
                    System.out.println("设置输入");

                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        listEt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                listEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});//限制输入6位
                return false;
            }
        });


    }
    public void requestFocus(View view) {
        listEt.setCursorVisible(true);
        listEt.requestFocus();
        listEt.setText("123456789");
    }

    public void clearFocus(View view) {
        listEt.clearFocus();
        listEt.setCursorVisible(false);
        listEt.setText("");
    }
    Comparator<MyClassBean> beanComparator=new Comparator<MyClassBean>() {
        @Override
        public int compare(MyClassBean o1, MyClassBean o2) {
            if (o1==null) {
                return 1;
            }
            if (o2==null) {
                return -1;
            }
            return o1.getName().compareTo(o2.getName());
        }
    };
    /** 判断是否是快速点击 */
    private static long lastClickTime;

    public static boolean isDoubleClick() {
        Log.e("shuagnji","current"+System.currentTimeMillis()+"lastClickTime+:"+lastClickTime+",差"+(System.currentTimeMillis()- lastClickTime));

        if (System.currentTimeMillis()- lastClickTime < 1000) {
            Log.e("shuagnji","11");
            return true;
        }
        lastClickTime = System.currentTimeMillis();
        Log.e("shuagnji","lastClickTime"+lastClickTime);
        return false;
    }
    public void scroollToTop(View view) {
        if (isDoubleClick()) {
            return;
        }
        Log.e("shuagnji","22");
        Collections.sort(myClassList,beanComparator);
        //设置listview滑动到顶部
        adapter.notifyDataSetChanged();
        fixListview.setSelection(0);

//        Intent intent = new Intent();
//        intent.setClass(ListViewActivity.this, HistoryDateSelecteActivity.class);
//        startActivity(intent);
    }

    void showShortToast(String content){
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        mToast=Toast.makeText(this,content,Toast.LENGTH_SHORT);
        mToast.show();
    }
    /** 判断是否是快速点击 */

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    /** 判断触摸时间派发间隔 */
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            if (isFastDoubleClick()) {
//                return true;
//            }
//        }
//        return super.dispatchTouchEvent(ev);
//    }



}